package cn.dazhou.wificonnectdemo.wifi

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.wifi.WifiManager
import cn.dazhou.wificonnectdemo.Timber

/** wifi开关状态接收器，用于接收wifi的打开和关闭 */
object WifiSwitchReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if (WifiManager.WIFI_STATE_CHANGED_ACTION == intent.action) {
            when (val state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)) {
                WifiManager.WIFI_STATE_ENABLING ->  Timber.fi("wifi正在打开...")
                WifiManager.WIFI_STATE_ENABLED -> { Timber.fi("wifi已打开: ${WifiUtil.getWifiName()}"); WifiScanHandler.startScan(); }
                WifiManager.WIFI_STATE_DISABLING -> Timber.fi("wifi正在关闭...")
                WifiManager.WIFI_STATE_DISABLED ->  Timber.fi("wifi已关闭")
                else ->                             Timber.fi( "未知wifi状态：$state")
            }
        }
    }

}